#pragma GCC optimize(3)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <vector>

using namespace std;
using LL = long long;
const int N = 1e5 + 10;


struct Node
{
    int l, r, cnt;
}node[N * 4 + N * 17];
int root[N], idx;
int A[N], que[N];

int build(int l, int r){
    int p = ++idx;
    node[p].l = 0, node[p].r = 0, node[p].cnt = 0;
    if(l == r) return p;
    int mid = l + r >> 1;
    node[p].l = build(l, mid), node[p].r = build(mid + 1, r);
    return p;
}

int insert(int p, int l, int r, int x){
    int q = ++idx;
    node[q] = node[p];
    if(l == r){
        node[q].cnt = node[p].cnt + 1;
        return q;
    }

    int mid = l + r >> 1;
    if(x <= mid) node[q].l = insert(node[p].l, l, mid, x);
    else node[q].r = insert(node[p].r, mid + 1, r, x);

    return q;
}


int query(int u, int l, int r, int k){
    int mid = l + r >> 1;
    if(l == r){
        return node[u].cnt;
    }

    if(k <= mid) return query(node[u].l, l, mid, k);
    else return query(node[u].r, mid + 1, r, k);
}


void solve(){
    int n, q;
    int T = 1;
    int maxv = 0, maxid = 0;
    scanf("%d%d", &n, &q);
    

    idx = 0;
    // memset(root, 0, sizeof root);
    root[0] = build(1, n);


    int hh = 0, tt = -1;
    for(int i = 1; i <= n; i ++){
        scanf("%d", &A[i]);
        if(A[i] > maxv){
            maxv = A[i];
            maxid = i;
        }
        que[++tt] = i;
    }

    int k1 = que[hh++];
    
    int finalT = 0;
    while(hh <= tt){
        int k2 = que[hh++];
        if(A[k1] > A[k2]){
            root[T] = insert(root[T - 1], 1, n, k1);
            T++;
        }else if(A[k1] < A[k2]){
            //A[k1] < A[k2]
            root[T] = insert(root[T - 1], 1, n, k2);
            k1 = k2;
            T++;
        }


        if(A[k1] == maxv){
            finalT = T - 1;
            break;
        }
        
    }

    while(q --){
        int num, k;
        scanf("%d%d", &num, &k);
        if(k > finalT){
            if(num != maxid){
                printf("%d\n", query(root[finalT], 1, n, num));
            }else{
                printf("%d\n", query(root[finalT], 1, n, num) + k - finalT);
            }
        }else{ //k <= finalT
            printf("%d\n", query(root[k], 1, n, num));
        }
    }

}

int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);

    int T;
    scanf("%d", &T);
    while(T--){
        solve();
    }
    return 0;
}